package com.szw.dlyk.web;

import com.github.pagehelper.PageInfo;
import com.szw.dlyk.domain.Activity;
import com.szw.dlyk.domain.User;
import com.szw.dlyk.query.ActivityQuery;
import com.szw.dlyk.result.Result;
import com.szw.dlyk.service.ActivityService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @program: dlyk
 * @description: 市场活动
 * @author: Songzw
 * @create: 2024-02-03 16:23
 **/
@RestController
public class ActivityController {

    @Resource
    private ActivityService activityService;

    /**
     * 市场活动分页查询
     * @param current
     * @return
     */
    @GetMapping("/api/activities")
    public Result getUsersByPage(@RequestParam(value = "current",required = false)Integer current,
                                 ActivityQuery activityQuery){
        if (current == null){
            current = 1;
        }
        PageInfo<Activity> activityByPage= activityService.getActivityByPage(current,activityQuery);
        return Result.ok(activityByPage);
    }

    @PostMapping(value = "/api/activity")
    public Result addActivity(ActivityQuery activityQuery, @RequestHeader(value = "Authorization") String token) {
        activityQuery.setToken(token);
        int save = activityService.saveActivity(activityQuery);
        return save >= 1 ? Result.ok():Result.fail();
    }

    @PutMapping(value = "/api/activity")
    public Result editActivity(ActivityQuery activityQuery, @RequestHeader(value = "Authorization") String token) {
        activityQuery.setToken(token);
        int update = activityService.updateActivity(activityQuery);
        return update >= 1 ? Result.ok():Result.fail();
    }

    @GetMapping(value = "/api/activity/{id}")
    public Result loadActivity(@PathVariable(value = "id") Integer id) {
        Activity activity = activityService.getActivityById(id);
        return Result.ok(activity);
    }
}
